package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DsXx;
import com.ruoyi.system.service.IDsXxService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 聊天记录Controller
 *
 * @author tyro
 * @date 2025-03-13
 */
@RestController
@RequestMapping("/system/xx")
public class DsXxController extends BaseController
{
    @Autowired
    private IDsXxService dsXxService;

    /**
     * 查询聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:xx:list')")
    @GetMapping("/list")
    public TableDataInfo list(DsXx dsXx)
    {
        startPage();
        List<DsXx> list = dsXxService.selectDsXxList(dsXx);
        Long userCount = dsXxService.getUserCount(dsXx);
        TableDataInfo tableDataInfo =getDataTable(list);
        tableDataInfo.setUserCount(userCount);  // 将用户总数添加到返回数据中
        return tableDataInfo;
    }

    /**
     * 导出聊天记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:xx:export')")
    @Log(title = "聊天记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DsXx dsXx)
    {
        List<DsXx> list = dsXxService.selectDsXxList(dsXx);
        ExcelUtil<DsXx> util = new ExcelUtil<DsXx>(DsXx.class);
        util.exportExcel(response, list, "聊天记录数据");
    }

    /**
     * 获取聊天记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:xx:query')")
    @GetMapping(value = "/{xxId}")
    public AjaxResult getInfo(@PathVariable("xxId") Long xxId)
    {
        return success(dsXxService.selectDsXxByXxId(xxId));
    }

    // 修改接口：接受 ltId 和 createTime
    @GetMapping("/aiResponse")
    public AjaxResult getAIResponse(@RequestParam("ltId") String ltId,
                                    @RequestParam("createTime") String createTime) {
        DsXx aiResponse = dsXxService.selectDsXxAIResponse(ltId, createTime);
        return success(aiResponse);
    }

    /**
     * 新增聊天记录
     */
    @PreAuthorize("@ss.hasPermi('system:xx:add')")
    @Log(title = "聊天记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DsXx dsXx)
    {
        return toAjax(dsXxService.insertDsXx(dsXx));
    }

    /**
     * 修改聊天记录
     */
    @PreAuthorize("@ss.hasPermi('system:xx:edit')")
    @Log(title = "聊天记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DsXx dsXx)
    {
        return toAjax(dsXxService.updateDsXx(dsXx));
    }

    /**
     * 删除聊天记录
     */
    @PreAuthorize("@ss.hasPermi('system:xx:remove')")
    @Log(title = "聊天记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{xxIds}")
    public AjaxResult remove(@PathVariable Long[] xxIds)
    {
        return toAjax(dsXxService.deleteDsXxByXxIds(xxIds));
    }
}
